#include "znx_module.h"
#include "comm/znx_log.h"


void
znx_modules_init()
{
    int i = 0;
    for ( ;; ) {
        if (core_modules[i] == NULL) {
            break;
        }

        core_modules[i]->index = i;
        i++;
    }

    core_modules_num = i;

    i = 0;
    for ( ;; ) {
        if (http_modules[i] == NULL) {
            break;
        }

        http_modules[i]->index = i;
        i++;
    }
    http_modules_num = i;
}


void
znx_modules_main_init_exec()
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->main_init) {
            if (!core_modules[i]->main_init()) {
                ZNX_DEF_LOG_FATAL(NULL, "module %v main init exec failed",
                    &core_modules[i]->name);
            }
        }
    }

    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->main_init) {
            if (!http_modules[i]->main_init()) {
                ZNX_DEF_LOG_FATAL(NULL, "module %v main init exec failed",
                    &http_modules[i]->name);
            }
        }
    }
}


void
znx_modules_main_exit_exec()
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->main_exit) {
            if (!core_modules[i]->main_exit()) {
                ZNX_DEF_LOG_FATAL(NULL, "module %v main exit exec failed",
                    &core_modules[i]->name);
            }
        }
    }

    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->main_exit) {
            if (!http_modules[i]->main_exit()) {
                ZNX_DEF_LOG_FATAL(NULL, "module %v main exit exec failed",
                    &http_modules[i]->name);
            }
        }
    }
}


znx_bool_t
znx_modules_worker_init_handle(void *data)
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->worker_init) {
            if (!core_modules[i]->worker_init()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker init exec failed",
                    &core_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->worker_init) {
            if (!http_modules[i]->worker_init()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker init exec failed",
                    &http_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}


znx_bool_t
znx_modules_worker_proc_exit_handle(void *data)
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->worker_proc_exit) {
            if (!core_modules[i]->worker_proc_exit()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker proc exec failed",
                    &core_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->worker_proc_exit) {
            if (!http_modules[i]->worker_proc_exit()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker proc exec failed",
                    &http_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}


znx_bool_t
znx_modules_worker_exit_handle(void *data)
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->worker_exit) {
            if (!core_modules[i]->worker_exit()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker exec failed",
                    &core_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->worker_exit) {
            if (!http_modules[i]->worker_exit()) {
                ZNX_DEF_LOG_ERROR(NULL, "module %v worker exec failed",
                    &http_modules[i]->name);
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}


znx_bool_t
znx_core_modules_conf_post_exec()
{
    for (int i = 0; i < core_modules_num; i++) {
        if (core_modules[i]->conf_post) {
            if (!core_modules[i]->conf_post()) {
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}


znx_bool_t
znx_http_module_main_conf_post_exec()
{
    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->main_conf_post) {
            if (!http_modules[i]->main_conf_post()) {
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}


znx_bool_t
znx_http_module_site_conf_post_exec(znx_http_site_conf_t *site_conf)
{
    for (int i = 0; i < http_modules_num; i++) {
        if (http_modules[i]->site_conf_post) {
            if (!http_modules[i]->site_conf_post(site_conf)) {
                return ZNX_FALSE;
            }
        }
    }

    return ZNX_TRUE;
}